package common.meters;

public class CountMeter {
    long maximum;
    long maximumDate;
    long count;

    public CountMeter()
    {
        count=0;
        reset();
    }

    synchronized public void reset()
    {
        maximum = count;
        maximumDate = System.currentTimeMillis();
    }
    synchronized public void decrement()
    {
        count--;
    }
    synchronized public void increment()
    {
        count++;
        if (count > maximum)
        {
            maximum = count;
            maximumDate = System.currentTimeMillis();
        }
    }
    synchronized public void change(long amount)
    {
        count+=amount;
        if (count > maximum)
        {
            maximum = count;
            maximumDate = System.currentTimeMillis();
        }
    }
    synchronized public void set(long amount)
    {
        count=amount;
        if (count > maximum)
        {
            maximum = count;
            maximumDate = System.currentTimeMillis();
        }
    }
    synchronized public boolean testAndSet(long testValue,long setValue)
    {
        if (count!=testValue)
        {
            return false;
        }
        count=setValue;
        if (count > maximum)
        {
            maximum = count;
            maximumDate = System.currentTimeMillis();
        }
        return true;
    }
    synchronized public boolean testAndIncrement(long testValue)
    {
        if (count!=testValue)
        {
            return false;
        }
        count++;
        if (count > maximum)
        {
            maximum = count;
            maximumDate = System.currentTimeMillis();
        }
        return true;
    }
    synchronized public boolean decrementIfGreater(final long value)
    {
        if (count > value)
        {
            count--;
            return true;
        }
        
        return false;
    }
    synchronized public boolean incrementIfLess(final long value)
    {
        if (count < value)
        {
            count++;
            if (count > maximum)
            {
                maximum = count;
                maximumDate = System.currentTimeMillis();
            }

            return true;
        }
        
        return false;
    }    
    synchronized public long getCount()
    {
            return count;
    }
    public long getMaximum()
    {
        return maximum;
    }
    public long getMaximumDate()
    {
       return maximumDate;
    }
    synchronized public CountMeter getSnapshot()
    {
    	CountMeter meter=new CountMeter();
    	meter.count = count;
        meter.maximum= maximum;
        meter.maximumDate = maximumDate;
        return meter;
    }
}
